package com.teradata.filter;


import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

//@WebFilter
public class HelloFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(">>filter init<<");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //实现功能：过滤所有请求，判断请求参数中是否包含“key”
        // 同时“key” == “xiaohong”， 如不包含则认为是非法请求，返回“param error”，如合法则继续访问。
        System.out.println(">>filter doFilter<<");
        String parameter = request.getParameter("key");
        if (null != parameter && (parameter.equals("xiaohong") || parameter.equals("xiaoming"))) {
            System.out.println(">>filter match<<");
            chain.doFilter(request, response);
        } else {
            System.out.println(">>filter param error<<");
            PrintWriter out = response.getWriter();
            out.print("param error");
            out.close();
        }
    }

    @Override
    public void destroy() {
        System.out.println(">>filter destory<<");
    }
}
